package cn.hh.wechatkit.api.card;

import cn.hh.wechatkit.data.returndata.Meta_RMsg_Base;
import cn.hh.wechatkit.data.postdata.card.Meta_CardSeatPostData;
import cn.hh.wechatkit.data.postdata.card.metaCardUpdateInfoPostData;
import cn.hh.wechatkit.data.postdata.card.metaCardUpdateStockPostData;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidResponse;
import cn.hh.wechatkit.pojo.card.Wx_Seat;
import cn.hhchat.kit.http.httpUtil;
import com.alibaba.fastjson.JSON;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import static cn.hh.wechatkit.helper.WxConst.ModifyStock_POST_URL;
import static cn.hh.wechatkit.helper.WxConst.UpdateCardInfo_POST_URL;
import static cn.hh.wechatkit.helper.WxConst.UpdateCardSeat_Post_URL;


/**
 * Created this one by HMH on 2017/5/7.
 */
public class WxAPI_Card_UpdateCard_Api {
    private static final Logger logger = LoggerFactory.getLogger(WxAPI_Card_UpdateCard_Api.class);

    //更新会议门票
    public static Meta_RMsg_Base updateSeat(String cardId, String code, Wx_Seat position, String ACCESS_TOKEN) throws IOException, Wx_Exception_InvalidResponse {
        String url = UpdateCardSeat_Post_URL.replace("ACCESS_TOKEN", ACCESS_TOKEN);
        //填充数据
        Meta_CardSeatPostData seat = new Meta_CardSeatPostData();
        seat.setCard_id(cardId);
        seat.setCode(code);
        seat.setEntrance(position.getEntrance());
        seat.setZone(position.getZone());
        seat.setSeat_number(position.getSeatNumber());
        //发送请求
        Response response = httpUtil.postJson(url, seat);
        //获取数据
        if (response != null) {
            String body = response.body().string();
            return JSON.parseObject(body, Meta_RMsg_Base.class);
        }
        throw new Wx_Exception_InvalidResponse("没有收到响应，或者请求出错");
    }

    //修改库存接口
    public static Meta_RMsg_Base updateQuantity(String card_id, String action, int number, String ACCESS_TOKEN) throws IOException, Wx_Exception_InvalidResponse {
        String url = ModifyStock_POST_URL.replace("ACCESS_TOKEN", ACCESS_TOKEN);
        metaCardUpdateStockPostData postData = new metaCardUpdateStockPostData(card_id, action, number);
        Response response = httpUtil.postJson(url, postData);
        //获取数据
        if (response != null) {
            String body = response.body().string();
            return JSON.parseObject(body, Meta_RMsg_Base.class);
        }
        throw new Wx_Exception_InvalidResponse("没有收到响应，或者请求出错");
    }

    //更改卡券信息接口
    public static Meta_RMsg_Base updateCustomUrl(String cardId, String customUrl, String ACCESS_TOKEN) throws IOException, Wx_Exception_InvalidResponse {
        String url = UpdateCardInfo_POST_URL.replace("ACCESS_TOKEN", ACCESS_TOKEN);
        metaCardUpdateInfoPostData postData = new metaCardUpdateInfoPostData();
        postData.setCard_id(cardId);
        postData.setCustomUrl(customUrl);
        Response response = httpUtil.postJson(url, postData);
        //获取数据
        if (response != null) {
            String body = response.body().string();
            return JSON.parseObject(body, Meta_RMsg_Base.class);
        }
        throw new Wx_Exception_InvalidResponse("没有收到响应，或者请求出错");
    }
}
